package org.example.Demo;

/*
import java.io.*;
import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class MyThread extends Thread{

    Socket socket;
    public MyThread (Socket socket){
        this.socket = socket;
    }

    @Override
    public void run() {
        try(
                OutputStream os = socket.getOutputStream();
                InputStream is = socket.getInputStream();

                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                PrintWriter writer = new PrintWriter(os);
                ){

            //先接受客户端请求过来的数据

            while (true){
                String s = reader.readLine();
                if (s.isEmpty()){
                    break;
                }
                System.out.println(s);
            }

            StringBuilder stringBuilder = new StringBuilder();
            //读取数据
            readData(stringBuilder);

            //接着再返回数据
            writer.println("HTTP/1.1 200 OK");
            writer.println("Content-Type:text/html;charset=UTF-8");
            writer.println();
            writer.println(stringBuilder);


        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void readData(StringBuilder stringBuilder){

        try(
                InputStream inputStream = new FileInputStream("D:\\software\\code\\2023_java\\2023_java_code\\code_24_5_8\\src\\main\\java\\org\\example\\Demo\\response.html");
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

        ){
            while (true){
                String s = reader.readLine();
                if ( s == null || s.isEmpty()){
                    break;
                }
                stringBuilder.append(s);
            }

        }catch (Exception e){
            e.printStackTrace();
        }


    }
}
*/


import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.io.*;
import java.net.Socket;

public class MyThread extends Thread{

    Socket socket;
    public MyThread (Socket socket){
        this.socket = socket;
    }

    @Override
    public void run() {
        try(
                OutputStream os = socket.getOutputStream();
                InputStream is = socket.getInputStream();

                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
        ){

            //先接受客户端请求过来的数据
            String s;
            while (!(s = reader.readLine()).isEmpty()){
                System.out.println(s);
            }

            StringBuilder stringBuilder = new StringBuilder();
            //读取数据
            readData(stringBuilder);

            //接着再返回数据
            writer.write("HTTP/1.1 200 OK\r\n");
            writer.write("Content-Type:text/html;charset=UTF-8\r\n");
            writer.write("\r\n");
            writer.write(stringBuilder.toString());


        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void readData(StringBuilder stringBuilder){

        try(
                InputStream inputStream = new FileInputStream("D:\\software\\code\\2023_java\\2023_java_code\\code_24_5_8\\src\\main\\java\\org\\example\\Demo\\response.html");
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

        ){
            while (true){
                String s = reader.readLine();
                if ( s == null || s.isEmpty()){
                    break;
                }
                stringBuilder.append(s);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
